const { observable, action } = require('mobx-miniprogram');
const teamRepository = require('../services/team_repository');
// 使用统一配置入口引用模型
const { TeamMemberModel } = require('../../config/models_config');

/**
 * 团队管理状态Store
 */
export const teamStore = observable({
  // 状态定义
  loading: false,
  error: null,
  teamMembers: [],
  currentMember: null,
  pagination: { page: 1, size: 50, total: 0 },
  filters: { role: 'all', department: 'all', search: '' },
  
  /**
   * 加载项目团队成员列表
   * @param {String} projectId 项目ID
   * @param {Object} params 查询参数
   */
  async getProjectTeam(projectId, params = {}) {
    if (!projectId) return;
    
    this.loading = true;
    this.error = null;
    
    try {
      // 合并筛选参数
      const queryParams = { ...this.filters, ...params };
      
      // 调用仓储层
      const result = await teamRepository.getProjectTeamList(projectId, queryParams);
      
      // 批量更新状态
      this.teamMembers = result.list || [];
      this.pagination = {
        page: result.page || 1,
        size: result.size || 50,
        total: result.total || 0
      };
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '加载团队成员列表失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 获取团队成员详情
   * @param {String} id 成员ID
   */
  async getTeamMemberDetail(id) {
    if (!id) return;
    
    this.loading = true;
    this.error = null;
    
    try {
      // 调用仓储层
      const member = await teamRepository.getTeamMemberById(id);
      
      // 更新状态
      this.currentMember = member;
      this.loading = false;
      
      return member;
    } catch (err) {
      this.error = err.message || '获取团队成员详情失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 添加团队成员
   * @param {Object} memberData 成员数据
   */
  async addTeamMember(memberData) {
    this.loading = true;
    this.error = null;
    
    try {
      // 转换为成员模型
      const member = new TeamMemberModel(memberData);
      
      // 调用仓储层
      const result = await teamRepository.addTeamMember(member);
      
      // 刷新列表
      await this.getProjectTeam(member.projectId);
      
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '添加团队成员失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 更新团队成员
   * @param {Object} memberData 成员数据
   */
  async updateTeamMember(memberData) {
    if (!memberData._id) throw new Error('更新成员时ID不能为空');
    
    this.loading = true;
    this.error = null;
    
    try {
      // 转换为成员模型
      const member = new TeamMemberModel(memberData);
      
      // 调用仓储层
      const result = await teamRepository.updateTeamMember(member);
      
      // 更新当前成员
      if (this.currentMember && this.currentMember._id === member._id) {
        this.currentMember = member;
      }
      
      // 刷新列表
      await this.getProjectTeam(member.projectId);
      
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '更新团队成员失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 移除团队成员
   * @param {String} memberId 成员ID
   * @param {String} projectId 项目ID
   */
  async removeTeamMember(memberId, projectId) {
    this.loading = true;
    this.error = null;
    
    try {
      // 调用仓储层
      const result = await teamRepository.removeTeamMember(memberId, projectId);
      
      // 刷新列表
      await this.getProjectTeam(projectId);
      
      // 清除当前成员
      if (this.currentMember && this.currentMember._id === memberId) {
        this.currentMember = null;
      }
      
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '移除团队成员失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 更新团队成员权限
   * @param {String} memberId 成员ID
   * @param {String} projectId 项目ID
   * @param {Object} permissions 权限配置
   */
  async updateMemberPermissions(memberId, projectId, permissions) {
    this.loading = true;
    this.error = null;
    
    try {
      // 调用仓储层
      const result = await teamRepository.updateMemberPermissions(memberId, projectId, permissions);
      
      // 刷新成员详情
      await this.getTeamMemberDetail(memberId);
      
      // 刷新列表
      await this.getProjectTeam(projectId);
      
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '更新权限失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 批量更新团队工作量分配
   * @param {String} projectId 项目ID
   * @param {Array} workloadData 工作量分配数据 [{memberId, workload}]
   */
  async updateTeamWorkload(projectId, workloadData) {
    this.loading = true;
    this.error = null;
    
    try {
      // 调用仓储层
      const result = await teamRepository.updateTeamWorkload(projectId, workloadData);
      
      // 刷新列表
      await this.getProjectTeam(projectId);
      
      this.loading = false;
      
      return result;
    } catch (err) {
      this.error = err.message || '更新工作量分配失败';
      this.loading = false;
      throw err;
    }
  },
  
  /**
   * 设置筛选条件
   * @param {Object} filters 筛选参数
   */
  setFilters: action(function(filters) {
    this.filters = { ...this.filters, ...filters };
    // 重置分页
    this.pagination.page = 1;
  }),
  
  /**
   * 重置当前成员
   */
  resetCurrentMember: action(function() {
    this.currentMember = null;
  })
}); 